﻿#include "js/json2.js"

#include "js/function.js"

#include "js/photoShop.js"

//创建标记图像颜色
var white = new SolidColor();
white.rgb["hexValue"] = "ffffff";
var black = new SolidColor();
black.rgb["hexValue"] = "000000";

//标记图像格式
var pngSaveOption = new PNGSaveOptions();
var jpegSaveOption = new JPEGSaveOptions();
var bitmapconversionoption = new BitmapConversionOptions();

// 用于拼接文件名
var falsifyfile = "falsify";
var markfile = "mark";

// 选择输入和输出的文件夹
var picFolder = Folder("D:\\学习\\大三上\\项目\\ps脚本项目\\photoshop-script\\input\\pics\\")
var jsonFolder = Folder("D:\\学习\\大三上\\项目\\ps脚本项目\\photoshop-script\\input\\json\\")
var outFolder = Folder("D:\\学习\\大三上\\项目\\ps脚本项目\\photoshop-script\\output\\");

// 切换为仿制图章工具
useCloneStamp();
// 设置笔触大小
setEraserSize(12)

// 遍历图片文件
var pictureList = picFolder.getFiles();
for (var picIndex = 0; picIndex < pictureList.length; picIndex++) {
    var onePicture = pictureList[picIndex];
    // 打开图片
    open(onePicture);
    var document = app.activeDocument;
    // 读取图片对应的json文件
    var jsonFileName = jsonFolder + "\\" + onePicture.name + ".json"
    var jsonFile = new File(jsonFileName);
    if (!jsonFile.open("r")) {
        throw ("tmp file open error");
    }
    var fileStr = jsonFile.read();
    var jsonStr = fileStrToJson(fileStr);


    try {
        lockPicture();
    }
    catch (e) {;}

    var arr = JSON.parse(jsonStr)
    var arrLength = arr.length;
    // 擦除数量
    var eraseBlockNum = randomNum(1, arrLength);

    // stateArr存放的是每个block是否被抹除，当为true时抹除
    var stateArr = [];
    for (var i = 0; i < arr.length; i++) {
        stateArr.push(false);
    }
    // 挑选eraseBlockNum个block进行抹除
    for (var i = 0; i < eraseBlockNum; i++) {
        while (true) {
            var randomOne = randomNum(0, arr.length - 1);
            if (stateArr[randomOne] == false) {
                stateArr[randomOne] = true;
                break;
            }
        }
    }

    // 用于存储擦除的矩形信息
    var blocks = [];

    for (var i = 0; i < arr.length; i++) {
        // 限制为被随机选中的矩形
        if (stateArr[i] == false)
            continue;
        var obj = arr[i];
        // alert(obj);
        var rect = obj['box']
        var startX = rect[0][0];
        var startY = rect[0][1];
        var endX = rect[2][0];
        var endY = rect[2][1];

        // 使用仿制图章工具进行擦除方块
        cloneStampBlock(startX, endX, startY, endY);

        // 存储擦除的矩形信息
        blocks.push(rect);
    }

    // 创建伪造图片文件夹和mask图文件夹
    createout(outFolder, falsifyfile);
    createout(outFolder, markfile);

    // 存储擦除后的图像
    var falsifyOut = new File(outFolder + "\\" + falsifyfile + "\\" + onePicture.name);
    // 获取图片后缀名
    var fileTail = getFileTail(onePicture.name);
    if (fileTail === "jpg") {
        document.saveAs(falsifyOut, jpegSaveOption);
    }
    else {
        document.saveAs(falsifyOut, pngSaveOption);
    }

    // 创建为位图【黑白mask图】
    app.documents.add(document.width, document.height, 72, getfilename(onePicture.name), NewDocumentMode.BITMAP, DocumentFill.TRANSPARENT, 1);
    //关闭原图像文档
    document.close(SaveOptions.DONOTSAVECHANGES);

    var markdocument = app.activeDocument;
    getMaskPic(markdocument, blocks);

    // 存储对应的mask图
    var markOut = new File(outFolder + "\\" + markfile + "\\" + getfilename(onePicture.name) + ".png");
    markdocument.saveAs(markOut, pngSaveOption);
    //关闭原mask图文档
    markdocument.close(SaveOptions.DONOTSAVECHANGES);
}



